package org.manusoft.refill.view;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.manusoft.refill.domain.TVehicleDetailsRf;
import org.manusoft.refill.dto.VehicleDO;
import org.manusoft.refill.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("createVehicle")
@Scope("request")
public class CreateVehicle implements Serializable{
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 881825193809174563L;

	private VehicleDO vehicle = new VehicleDO();
	public CreateVehicle() {}
	private TVehicleDetailsRf vehicleDetailsRf;
	
	private VehicleService vehicleService;
	
	@Autowired
	public CreateVehicle(VehicleService vehicleService) {
		this.vehicleService = vehicleService;
	}
	
	public TVehicleDetailsRf getVehicleDetailsRf(){
		return this.vehicleDetailsRf;
	}

	public void setRefillingDetailsRf(TVehicleDetailsRf vehicleDetailsRf) {
		this.vehicleDetailsRf = vehicleDetailsRf;
	}
	
	public void save(ActionEvent actionEvent) {
		vehicleDetailsRf = new TVehicleDetailsRf();
		
		vehicleDetailsRf.setVehCharInsurance(vehicle.getVehCharInsurance());
		vehicleDetailsRf.setVehCharMake(vehicle.getVehCharMake());
		vehicleDetailsRf.setVehCharModel(vehicle.getVehCharModel());
		vehicleDetailsRf.setVehCharOwner(vehicle.getVehCharOwner());
		vehicleDetailsRf.setVehCharRegno(vehicle.getVehCharRegno());
		vehicleDetailsRf.setVehDateInsurexpiry(vehicle.getVehDateInsurexpiry());
		vehicleDetailsRf.setVehDateRegdate(vehicle.getVehDateRegdate());
		vehicleDetailsRf.setVehNumId(vehicle.getVehNumId());
		vehicleDetailsRf.setVehFillingCapacity(vehicle.getVehNumTonnage());

		FacesMessage facesMessage = null;
		if(vehicleService.findByRegNum(vehicle.getVehCharRegno())!=null){
			facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Vehicle Reg Num alredy registered. Please add another Number");
		}else{
			vehicleService.createUpdateVehicle(vehicleDetailsRf);
			facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Vehicle is saved");
		}		

		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
//		vehicleDetailsRf = new TVehicleDetailsRf();
	}

	/**
	 * @return the vehicle
	 */
	public VehicleDO getVehicle() {
		return vehicle;
	}

	/**
	 * @param vehicle the vehicle to set
	 */
	public void setVehicle(VehicleDO vehicle) {
		this.vehicle = vehicle;
	}	
}